package com.see.domain;

import java.math.BigDecimal;

public class Price {

	private final BigDecimal value;
	//private final long value;
    private final boolean market;

    public Price(BigDecimal value) {
        this.value = value;
        this.market = false;
    }

    public Price() {
        this.value = BigDecimal.ZERO;
        this.market = true;
    }

    public BigDecimal getValue() {
        return value;
    }
    
    public boolean isMarket() {
    	return this.market;
    }

    public int compareTo(Price p) {
    	BigDecimal comparable = p.value;
    	
    	return getValue().compareTo(comparable);

//        if (getValue() == comparable) {
//            return 0;
//        } else if (getValue() < comparable) {
//            return -1;
//        } else if (getValue() > comparable) {
//            return 1;
//        } else {
//            return 0;
//        }
    }

    public boolean equals(Price p) {
    	if (this == p)
			return true;
        if (isMarket() && p.isMarket()) {
            return false;
        } else if(compareValueAndMarket(p)){
        	return true;
        } else if (compareTo(p) == 0) {
            return true;
        } else {
            return false;
        }
    }

    public boolean compareValueAndMarket(Price p){
    	if(isMarket() && !p.isMarket()){
    		return true;
    	} else if(!isMarket() && p.isMarket()){
    		return true;
    	}
    	return false;
    }
    
    public boolean lessOrEquals(Price p){
    	if (isMarket() && p.isMarket()) 
            return false;
    	if(compareTo(p) < 0 )
    		return false;
    	return true;
    }
    
    
    public boolean moreOrEquals(Price p){
    	if (isMarket() && p.isMarket()) 
            return false;
    	if(compareTo(p) > 0)
    		return false;
    	return true;
    }
 
    
    public static BigDecimal getMinPrice(Price p1, Price p2){
    	if(p1.isMarket())
    		return p2.value;
    	if(p2.isMarket())
    		return p1.value;
    	return p1.value.min(p2.value);
    	//return Math.min(p1.value, p2.value);
    }

    @Override
    public String toString() {
        if (isMarket()) {
            return "market";
        } else {
            return String.valueOf(getValue());
        }
    }	

}
